// pages/choose/choose.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // question: '1.通常微信群聊达到多少人时，必须得到被邀请人的同意才能进群？',
    question: '',
    items: [
      {value: '', name: ''},
      {value: '', name: ''},
      {value: '', name: ''},
      {value: '', name: ''},
    ],
    titleItems: [
      {value: '', name: '1'},
      {value: '', name: '2'},
      {value: '', name: '3'},
      {value: '', name: '4'},
      {value: '', name: '5'},
      {value: '', name: '6'},
      {value: '', name: '7'},
      {value: '', name: '8'},
      {value: '', name: '9'},
      {value: '', name: '10'},
    ],
    index: '',
    item: [],
    percentValue: 10,
    showModal: true,
    yesanswer: 0,
    noanswer:  10,
    minute: '',
    second: '',
    loading: false,
    lastQuestionDisabled: false,
  },
  // 计时器
  setInterval: function () {
    const that = this   
    var second = wx.getStorageSync('time')[1]
    var minute = wx.getStorageSync('time')[0]
    var timestopid    
    var show = function() { // 设置定时器
      if (second == 0 && minute == 0){
        wx.showLoading({
          title: '交卷中',
        })
        let accountId = wx.getStorageSync('accountId')
        let paperId = wx.getStorageSync('paperId')
        wx.request({
          url: 'http://112.74.105.17:20003/question-bank/wx/exam/countPoint',
          method: 'POST',
          data:{
            "accountId": accountId,
            "answerTotal": 0,
            "paperId": paperId,
            "point": 0,
            "total": 0
          },
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            console.log(res.data)
            wx.clearStorageSync()
            wx.hideLoading({
              success: (res) => {
                // 这里写跳转
              },
            })
          }
        })
        clearInterval(timestopid)
        return 0
      }
      second--
      if (second < 0) {
          second = 59 
          minute--
          if (minute < 10) {
              that.setData({
                  minute: '0' + minute,
                  second: second
              })
          } else {
              that.setData({
                  minute: minute,
                  second: second
              })
          }
      }
      if (second < 10) {
          that.setData({
              second: '0' + second,
              minute: minute
          })
      } else {
          that.setData({
            second: second,
            minute: minute
          })
      }     
      that.setData({
        loading: true
      })
      wx.hideLoading()
    }
    timestopid = setInterval(show, 1000)
  },
  // 交卷
  handpage: function() {
    let accountId = wx.getStorageSync('accountId')
    let paperId = wx.getStorageSync('paperId')
    wx.showLoading({
      title: '交卷中',
    })
    wx.request({
      url: 'http://112.74.105.17:20003/question-bank/wx/exam/countPoint',
      method: 'POST',
      data:{
        "accountId": accountId,
        "answerTotal": 0,
        "paperId": paperId,
        "point": 0,
        "total": 0
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        let point = res.data.data.point
        wx.clearStorageSync()
        wx.hideLoading({
          success: (res) => {
            // 这里写跳转
            wx.redirectTo({
              url: '../score/score?point='+ point,
            })
          },
        })
      }
    })
  },
//答题卡选项跳转
  answerJump: function(e) {
    let index = Number(e.currentTarget.dataset.name) - 1
    wx.setStorage({
      data: index,
      key: 'index',
    })
    let time = new Array()
    time[0] = this.data.minute
    time[1] = this.data.second
    wx.setStorage({
      data: time,
      key: 'time',
    })
    if (this.data.item[index].type == "SINGLE_CHOICE") {
      wx.redirectTo({
        url: '/pages/choose/choose',
      })
    }else if (this.data.item[index].type == "MULTIPE_CHOICE") {
      wx.redirectTo({
        url: '/pages/Multiple/Multiple',
      })
    }else if (this.data.item[index].type == "JUDGE") {
      wx.redirectTo({
        url: '/pages/judge/judge',
      })
    }
  },
  //答题卡选项颜色和题数
  answerColor: function() {
    let checked = wx.getStorageSync('checked') 
    let count = 0
    console.log(checked.length)
    console.log(checked[0].length)
    for (var i = 0; checked.length > i; ++i){
      for(var j = 0; checked[i].length > j; ++j){
        if(checked[i][j].checked == true){
          this.data.titleItems[i].value = Number(i) + 1
          count ++;
          break
        }
      }
    }
    this.setData({
      titleItems: this.data.titleItems,
      yesanswer: count,
      noanswer: this.data.titleItems.length - count,
    });
  },
  // 关闭答题卡
  answerClose: function() {
    this.setData({
      showModal: true,
    })
  },
  //显示答题卡
  showModalChange: function() {
    this.answerColor()
    this.setData({
      showModal: false,
    })
  },
  // 上一题按钮
  lastQuestion: function() {
    let index = Number(this.data.index) - 1
    let time = new Array()
    time[0] = this.data.minute
    time[1] = this.data.second
    if(this.data.index != 0){
      wx.setStorage({
        data: index,
        key: 'index',
      })
      wx.setStorage({
        data: time,
        key: 'time',
      })
      if (this.data.item[index].type == "SINGLE_CHOICE") {
        wx.redirectTo({
          url: '/pages/choose/choose',
        })
      }else if (this.data.item[index].type == "MULTIPE_CHOICE") {
        wx.redirectTo({
          url: '/pages/Multiple/Multiple',
        })
      }else if (this.data.item[index].type == "JUDGE") {
        wx.redirectTo({
          url: '/pages/judge/judge',
        })
      }
    }
  },
  // 下一题按钮
  nextQuestion: function() {
    let index = Number(this.data.index) + 1
    let time = new Array()
    time[0] = this.data.minute
    time[1] = this.data.second
    if (this.data.index != Number(this.data.item.length) - 1){
      wx.setStorage({
        data: index,
        key: 'index',
      })
      wx.setStorage({
        data: time,
        key: 'time',
      })
      if (this.data.item[index].type == "SINGLE_CHOICE") {
        wx.redirectTo({
          url: '/pages/choose/choose',
        })
      }else if (this.data.item[index].type == "MULTIPE_CHOICE") {
        wx.redirectTo({
          url: '/pages/Multiple/Multiple',
        })
      }else if (this.data.item[index].type == "JUDGE") {
        wx.redirectTo({
          url: '/pages/judge/judge',
        })
      }
    } else {
      wx.showLoading({
        title: '交卷中',
      })
      let paperId = wx.getStorageSync('paperId')
      let accountId = wx.getStorageSync('accountId')
      wx.request({
        url: 'http://112.74.105.17:20003/question-bank/wx/exam/countPoint',
        method: 'POST',
        data:{
          "accountId": accountId,
          "answerTotal": 0,
          "paperId": paperId,
          "point": 0,
          "total": 0
        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res.data)
          let point = res.data.data.point
          wx.clearStorageSync()
          wx.hideLoading({
            success: (res) => {
              // 这里写跳转
              wx.redirectTo({
                url: '../score/score?point='+ point,
              })
            },
          })
        }
      })
    }
  },
  checkboxChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    let submitAnswer = new Array()
    let stemId
    var items = this.data.items
    var values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false

      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === Number(values[j])) {
          items[i].checked = true
          submitAnswer.push(items[i].choiceId)
          stemId = items[i].stemId
          break
        }
      }
    }
    var arr = new Array(); 
    arr = wx.getStorageSync('checked') 
    arr[Number(this.data.index)] = items
    wx.setStorage({
      data: arr,
      key: 'checked',
    })
    console.log(items)
    this.setData({
      items: items
    })
    let accountId = wx.getStorageSync('accountId')
    let paperId = wx.getStorageSync('paperId')
    wx.request({
      url: 'http://112.74.105.17:20003/question-bank/wx/exam/saveSingleExam',
      method: 'POST',
      data:{
        'accountId': accountId,
        "answer": submitAnswer,
        "paperId": paperId,
        "questionType": "SINGLE_CHOICE",
        "stemId": stemId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
      }
    })
  },

  getmypapers(gradeName,subject){
    var that=this;
    wx.request({
      url: 'http://112.74.105.17:20003/question-bank/wx/exam/findExamList',
      method:'POST',
      data:{
        "gradeName": gradeName,
        "groupName": "小学",
        "subject": subject,
        "tenantId": "746122920027226112"
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        that.getmytopics(res.data.data[0].id);   
    }
  })
  },
  getmytopics(id){
  wx.request({
    url: 'http://112.74.105.17:20003/question-bank/wx/exam/paper-stem/'+id,
    method:'POST',
    data:{
    },
    header: {
      'content-type': 'application/json'
    },
    success (res) {
      console.log(res.data.data);
    }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let lastQuestionDisabled
    wx.showLoading({
      title: '加载中',
      mask: true,
    }) 
    let item = wx.getStorageSync('data')
    item = JSON.parse(item)
    let index = wx.getStorageSync('index')
    let checked = wx.getStorageSync('checked')
    // 定时器启动
    that.setInterval()
    if (index == 0){
      lastQuestionDisabled = true
    }
    if (checked[index][0] != undefined){
      for(var i = 0; item[index].optionDtoList.length > i; i++){
        that.data.items[i].checked = checked[index][i].checked
      }
    }
    for(var i = 0; item[index].optionDtoList.length > i; i++){
      that.data.items[i].choiceId = item[index].optionDtoList[i].id
      that.data.items[i].stemId = item[index].optionDtoList[i].stemId
      that.data.items[i].name = item[index].optionDtoList[i].content
      that.data.items[i].value = i
    }
    that.setData({
      question: item[index].name,
      items: that.data.items,
      item: item,
      index: index,   
      percentValue: (Number(index) + 1) * 10,
      checked: checked,
      lastQuestionDisabled: lastQuestionDisabled
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})